var appInfoDialog = appInfoDialog || {};
appInfoDialog.action = 0; // 操作：1,新增;2,修改
appInfoDialog.oldApp = {}; // 修改操作保存的旧数据
appInfoDialog.dialogUrl = ''; // 窗口操作url
appInfoDialog.successNote = ''; // 操作成功note
appInfoDialog.openDialog = function () {
	$('#add-dlg').dialog('open');
};
appInfoDialog.openAdd = function () {
	appInfoDialog.action = 1;
	$("#add-dlg").dialog({title: '新增'});
	$("#loginId").removeAttr("readonly");
	appInfoDialog.dialogUrl = contextPath+"/console/user/add";
	appInfoDialog.successNote = '添加成功';
	appInfoDialog.openDialog();
};
appInfoDialog.openUpdate = function (app) {
	if (!app) {
		apexLayer.Error('请先选择一条记录，才能进行修改操作！',null,false,1000);
		return;
	}
	var id = app.id;
	if (id == null || id == '') {
		apexLayer.Error('id为空');
		return;
	}
	appInfoDialog.oldApp = app;
	appInfoDialog.action = 2;
	$("#add-dlg").dialog({title: '修改'});
	$("#loginId").attr("readonly", "readonly");
	appInfoDialog.dialogUrl = contextPath+"/console/user/uptSystemUserInfo";
	appInfoDialog.successNote = '修改成功';
	for (var key in app) {
		var $element = $("#" + key);
		if ($element.hasClass("easyui-numberbox")) {
			$element.textbox('setValue', app[key]);
		} else {
			$("#" + key).val(app[key]);
		}
	}
	appInfoDialog.openDialog();
};

appInfoDialog.openMenuUpdate = function (app) {
	if (!app) {
		apexLayer.Error('请先选择一条记录，才能进行修改操作！',null,false,1000);
		return;
	}
	var loginId = app.loginId;
	if (loginId == null || loginId == '') {
		apexLayer.Error('登录ID为空');
		return;
	}
	$('#tt').tree({
	    url:contextPath+"/console/user/loadMenuTree?loginId="+loginId,
	    method:'get',
	    animate:true,
	    checkbox:true
	});
	appInfoDialog.oldApp = app;
	appInfoDialog.action = 2;
	appInfoDialog.dialogUrl = contextPath+"/console/user/updatePermission?loginId="+loginId;
	appInfoDialog.successNote = '更改权限成功';
	$('#update-dlg').dialog('open');
};
appInfoDialog.getChecked = function() {
	var solids =  $('#tt').tree('getChecked', ['checked','indeterminate']);
	var nodes = [];
	$.each(solids,function(){
		nodes.push(this.id);
	});
	return nodes.join(",");
}
$(function() {
	$("#add-dlg").dialog({
		width: 600,
		height: 'auto',
		closed: true,
		cache: false,
		modal: true,
		shadow: false,
		onClose: function () {
			$('#addForm input[name]').val('');
			$('#addForm .easyui-filebox').textbox('clear');
			$('#addForm .easyui-numberbox').textbox('clear');
		},
		buttons: [{
			text: '确定',
			iconCls:'icon-ok',
			handler: function () {
				$('#addForm').form('submit',{
		            url:appInfoDialog.dialogUrl,
		            onSubmit: function () {
		            	if (!$(this).form('validate')){
		            		return false;
		            	}else {
		            		return true;
		            	}
		            },
		            success:function(data){
		            	var obj = jQuery.parseJSON(data);
		            	if (obj.success) {
							apexLayer.Msg(appInfoDialog.successNote,1000,function(){
								$("#add-dlg").dialog('close');
								self.window.location = contextPath+"/console/user/list";
							});
						} else {
							apexLayer.Error(obj.note);
						}
		            }
		        });
			}
		}, {
			text: '关闭',
			iconCls:'icon-cancel',
			handler: function () {
				$("#add-dlg").dialog('close');
			}
		}]
	});
	$("#update-dlg").dialog({
		title:'用户菜单权限修改',
		width: 600,
		height: 500,
		closed: true,
		cache: false,
		modal: true,
		shadow: false,
		onClose: function () {
		},
		buttons: [{
			text: '确定',
			iconCls:'icon-ok',
			handler: function () {
				Ajax({
					url:appInfoDialog.dialogUrl,
					isMask : true,
					data: {
						menuIds :appInfoDialog.getChecked()	
					},
					success: function (data) {
						if (data.success) {
							apexLayer.Msg(appInfoDialog.successNote,1000,function(){
								$("#update-dlg").dialog('close');
							});
						} else {
							apexLayer.Error(data.note);
						}
					}
				});
			}
		}, {
			text: '关闭',
			iconCls:'icon-cancel',
			handler: function () {
				$("#update-dlg").dialog('close');
			}
		}]
	});
});